﻿using IntoStationSystem.Db;
using IntoStationSystem.Enums;
using IntoStationSystem.Views;
using Prism.Mvvm;

namespace IntoStationSystem.ViewModels
{
    public class LoginViewModel : BindableBase
    {
        public LoginViewModel() 
        {
            //DbHelper.Log("LoginViewModel");
            
        }

        private Authority curAuthority;

        private int userIndex = 0;

        private bool isShowPasswordErr = false;

        private HomeViewModel home => Locator.Instance.MainPageVM;

        public int UserIndex
        {
            get => userIndex;
            set
            {
                if (userIndex != value)
                {
                    userIndex = value;
                    curAuthority = (Authority)userIndex;
                    Locator.Instance.MainWindowVM.UseCfg = value == 2;
                }
            }
        }

        public bool IsShowPasswordErr
        {
            get => isShowPasswordErr;
            set
            {
                isShowPasswordErr = value;
                RaisePropertyChanged(nameof(IsShowPasswordErr));
            }

        }

        public string Password { get; set; } = string.Empty;

        public bool Login()
        {
            string originalPassword = string.Empty;
            switch (curAuthority)
            {
                case Authority.Operator:
                    Locator.Instance.MainPageVM.Authority = "操作员";
                    break;
                case Authority.Manager:
                    Locator.Instance.MainPageVM.Authority = "管理员";
                    originalPassword = "admin";
                    break;
                case Authority.Manufacturer:
                    Locator.Instance.MainPageVM.Authority = "制造商";
                    originalPassword = "into6306";
                    break;
                default: break;
            }
            if (Password == originalPassword)
            {
                IsShowPasswordErr = false;
                if (UserIndex >= 2)
                {
                    HomeView.RstCodeBtnShow();
                }
                return true;
            }
            else
            {
                IsShowPasswordErr = true;
                return false;
            }
        }
    }
}
